package com.zxlspace.collection2;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * 简化迭代器实现原理，用迭代器实现Arraylist
 * 
 */
public class MyArrayList2 {
    
    private String[] array = new String[]{"a","b","c","d","e","f"};

    private int size = array.length;

    private int cusor = -1;  //游标位置

    public boolean hasNext() {
        return cusor+1 < size;
    }

    //返回下一个元素,游标往下移动一个位置
    public String next() {
        cusor++;
        return array[cusor];
    }

    public void remove() {

    }

    public static void main(String[] args) {
        MyArrayList2 arrayList2 = new MyArrayList2();
        while (arrayList2.hasNext()) {
            System.out.println(arrayList2.next());
            System.out.println("当前游标位置：" + arrayList2.cusor);
        }

        //当前迭代器每次重复迭代需要重新new一个MyArrayList2
        MyArrayList2 arrayList3 = new MyArrayList2();
        while (arrayList3.hasNext()) {
            System.out.println(arrayList3.next());
        }

        List<String> list = new ArrayList<>();
        list.add("1");
        list.add("2");
        list.add("3");

        Iterator iterator = list.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
            iterator.remove();
        }

        /*Iterator iterator2 = list.iterator();
        while (iterator2.hasNext()) {
            System.out.println(iterator2.next());
        }*/
    }
}
